local pingyi = fk.CreateSkill{
  name = "rfenghou_3d18j__pingyi",
}

pingyi:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player.phase == Player.Play and player:hasSkill(pingyi.name) and data:isOnlyTarget(data.to) then
      local room = player.room
      local logic = room.logic
      local current_use = logic:getMostRecentEvent(GameEvent.UseCard)
      local events = logic:getEventsOfScope(GameEvent.UseCard, 2, function(e)
        return e.data and e.data.from == player
      end, player.HistoryPhase)
      return table.contains(events, current_use)
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = pingyi.name,
      prompt = "#rfenghou_3d18j__pingyi-invoke:" .. data.to.id,
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = data.to
    local result = 1
    if not to:isKongcheng() then
      result = #room:askToDiscard(to, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = pingyi.name,
        prompt = "#rfenghou_3d18j__pingyi-discard:" .. player.id,
      }) + 1
    end
    if result == 1 then
      player:drawCards(1, pingyi.name)
    end
    local old_mark = player:getMark("rfenghou_3d18j__pingyi-phase-noclear")
    room:setPlayerMark(player, "rfenghou_3d18j__pingyi-phase-noclear", result)
    local phase = room.logic:getMostRecentEvent(GameEvent.Phase)
    if phase and old_mark ~= 0 and old_mark ~= result and room:askToSkillInvoke(player, {
      skill_name = pingyi.name,
      prompt = "#rfenghou_3d18j__pingyi-reset",
    }) then
      room:notifySkillInvoked(player, pingyi.name)
      phase.data.phase_end = true
      player:gainAnExtraPhase(phase.data.phase, phase.data.reason, true, phase.data.extra_data)
      room:sendLog{
        type = "#Rfenghou_3d18jResetPhase",
        from = player.id,
        arg = Util.PhaseStrMapper(phase.data.phase),
      }
    end
  end
})

Fk:loadTranslationTable{
  ["rfenghou_3d18j__pingyi"] = "平夷",
  [":rfenghou_3d18j__pingyi"] = "出牌阶段，当你使用的前两张牌指定唯一目标后，你可以令其选择一项：其弃置一张牌；或令你摸一张牌。若两次选项不同，你可以<a href=':rfenghou_3d18j__reset_phase'>重新开始本阶段</a>。",

  [":rfenghou_3d18j__reset_phase"] = "结束本阶段，然后获得一个和这个阶段一样的阶段。",
  ["#Rfenghou_3d18jResetPhase"] = "%from 重新开始了 %arg",

  ["#rfenghou_3d18j__pingyi-invoke"] = "平夷：你可以令 %src 选择弃牌或令你摸牌",
  ["#rfenghou_3d18j__pingyi-discard"] = "平夷：你需弃置一张牌，否则 令 %src 摸一张牌",
  ["#rfenghou_3d18j__pingyi-reset"] = "平夷：你可以重新开始本阶段",
}

return pingyi
